package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
    ImageView L1;
    Button bnt1;
    boolean isLightOn1 = false; // 指示灯状态

    ImageView L2;
    Button bnt2;
    boolean isLightOn2 = false; // 指示灯状态

    ImageView L3;
    Button bnt3;
    boolean isLightOn3 = false; // 指示灯状态

    HardControl hardControl = new HardControl();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bnt1 = findViewById(R.id.button1);//  按钮灯开
        bnt2 = findViewById(R.id.button2);//  按钮灯开
        bnt3 = findViewById(R.id.button3);//  按钮灯开

        hardControl.ledOpen();
        bnt1.setOnClickListener(this);
        bnt2.setOnClickListener(this);
        bnt3.setOnClickListener(this);
        L1 = (ImageView)this.findViewById(R.id.Lamp1);
        L2 = (ImageView)this.findViewById(R.id.Lamp2);
        L3 = (ImageView)this.findViewById(R.id.Lamp3);
    }

    @Override
    public void onClick(View view) {


        int i=view.getId();

        if (i==R.id.button1)
        {
            if(isLightOn1)
            {
                L1.setVisibility(View.INVISIBLE);
                hardControl.ledCtrl_off(18,1);
                isLightOn1 = false;
            }
            else
            {
                L1.setVisibility(View.VISIBLE);
                hardControl.ledCtrl_on(18,0);
                isLightOn1 = true;
            }

        }
        else if (i==R.id.button2)
        {
            if(isLightOn2)
            {
                L2.setVisibility(View.INVISIBLE);
                hardControl.ledCtrl_off(20,1);
                isLightOn2 = false;
            }
            else
            {
                L2.setVisibility(View.VISIBLE);
                hardControl.ledCtrl_on(20,0);
                isLightOn2 = true;
            }
        }
        else if (i==R.id.button3)
        {
            if(isLightOn3)
            {
                L3.setVisibility(View.INVISIBLE);
                hardControl.ledCtrl_off(22,1);
                isLightOn3 = false;
            }
            else
            {
                L3.setVisibility(View.VISIBLE);
                hardControl.ledCtrl_on(22,0);
                isLightOn3 = true;
            }
        }


    }

    protected void onDestroy()
    {
        super.onDestroy();
        // 当Activity销毁时，关闭LED控制
        hardControl.ledCtrl_close(18);
        hardControl.ledCtrl_close(20);
        hardControl.ledCtrl_close(22);
    }

}